INTRODUCCIÓN

En el conjunto de datos se ha detectado que algunas variables están disponibles para distintos estados del tratamiento. Para los modelos que vamos a utilizar, podría ser más adecuado introducir un resumen de estas variables. Por ello, vamos a estudiarlas gráficamente y extraer conclusiones para cada una de ellas.

LIMPIEZA DE DATOS

Vamos a quedarnos tan solo con las variables de interes, el resto las eliminamos.

# Carga de datos
data = read.csv('datos_limpios.csv')

# Eliminamos las columnas que no están duplicadas
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data <- data %>%
  select(
    -Sex, -Age_at_diagnosis, -Elderly, -ECOG, -BMI,
    -Percentage_weight_loss, -Weight_loss_yes_no, -Smoking_habit,
    -Smoking_exposure, -Diabetes, -Cardiopathy, -Neurodegenerative_disease,
    -Histology, -Histology_num, -Stage, -Stage_num, -PD_L1, -Mutation_status,
    -Statins, -Total_cholesterol, -LDH, -CRP, -ALI_pre, -Number_of_cycles,
    -Treatment_interruption, -Interruption_reason, -Progression,
    -Second_line_treatment,-PFS, -PFS_censored, -Education,
    -Household
  )

data

ANÁLISIS VARIABLE A VARIABLE

Vamos a ir, variable a variables, estudiando su comportamiento temporal y correlación, veremos si existe mucha correlación o no entre ellas, con la idea de agruparlas dado el caso.

Proteina

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ readr     2.1.5
## ✔ ggplot2   3.5.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
vars_proteina <- c("Total_protein", "Protein_1C", "Protein_2C", "Protein_1eval")

df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_proteina)) %>%
  pivot_longer(cols = all_of(vars_proteina), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_proteina))  # <- AQUÍ está la clave

g1 <- ggplot(df_long1, aes(x = Momento, y = Valor, group = Patient_ID, color = as.factor(Mejor_resp))) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de proteína por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor proteína",
    color = "Mejor respuesta"
  )

ggplotly(g1)
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_proteina)) %>%
  pivot_longer(cols = all_of(vars_proteina), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_proteina))  # <- Orden correcto

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval)
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de proteína por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor proteína",
    color = "First_eval"
  )

ggplotly(g2)
library(GGally)
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
# Selección de las variables a correlacionar
vars_proteina <- c("Total_protein", "Protein_1C", "Protein_2C", "Protein_1eval")
df_corr <- data %>% select(all_of(vars_proteina))

# Gráfico de correlación
ggcorr(df_corr, 
       label = TRUE, 
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (proteínas)")

Correlaciones moderadas-altas (0.60–0.78) → variables relacionadas pero no idénticas. Se puede considerar resumir, aunque podrían aportar info complementaria.

Albumin

# Variables longitudinales de Albumina
vars_Albumin <- c("Albumin", "Albumin_1C", "Albumin_2C", "Albumin_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Albumin)) %>%
  pivot_longer(cols = all_of(vars_Albumin), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Albumin))  # Orden explícito

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de Albumina por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor Albumina",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Albumin)) %>%
  pivot_longer(cols = all_of(vars_Albumin), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Albumin))  # Orden explícito

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de Albumina por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor Albumina",
    color = "First_eval"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_Albumin))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (Albumina)")

Hemoglobin

vars_Hemo <- c("Hemoglobin", "Hemoglobin_1C", "Hemoglobin_2C", "Hemoglobin_1eval")

# Gráfico 1 - Mejor_resp
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Hemo)) %>%
  pivot_longer(cols = all_of(vars_Hemo), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Hemo))

g1 <- ggplot(df_long1, aes(
  x = Momento, y = Valor,
  group = Patient_ID, color = as.factor(Mejor_resp)
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de Hemo por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento", y = "Valor Hemo",
    color = "Mejor respuesta"
  )

ggplotly(g1)
# Gráfico 2 - First_eval
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Hemo)) %>%
  pivot_longer(cols = all_of(vars_Hemo), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Hemo))

g2 <- ggplot(df_long2, aes(
  x = Momento, y = Valor,
  group = Patient_ID, color = as.factor(First_eval)
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de Hemo por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento", y = "Valor Hemo",
    color = "First_eval"
  )

ggplotly(g2)
# Matriz de correlación
df_corr <- data %>% select(all_of(vars_Hemo))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (Hemo)")

Alta correlación

Leukocytes

# Variables longitudinales de leucocitos
vars_Leuko <- c("Total_leukocytes", "Leukocytes_1C", "Leukocytes_2C", "Leukocytes_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Leuko)) %>%
  pivot_longer(cols = all_of(vars_Leuko), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Leuko))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de leucocitos por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor leucocitos",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por 1ª_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Leuko)) %>%
  pivot_longer(cols = all_of(vars_Leuko), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Leuko))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de leucocitos por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor leucocitos",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_Leuko))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (leucocitos)")

Alta correlación, excepto el Total con el Final de los leukocitos

Neutrofilos

# Variables longitudinales de neutrófilos
vars_Neutro <- c("Neutrophils", "Neutrophils_1C", "Neutrophils_2C", "Neutrophils_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Neutro)) %>%
  pivot_longer(cols = all_of(vars_Neutro), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Neutro))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de neutrófilos por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor neutrófilos",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Neutro)) %>%
  pivot_longer(cols = all_of(vars_Neutro), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Neutro))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de neutrófilos por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor neutrófilos",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_Neutro))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (neutrófilos)")

Linfocitos

# Variables longitudinales de linfocitos
vars_Lympho <- c("Total_lymphocytes", "Lymphocytes_1C", "Lymphocytes_2C", "Lymphocytes_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Lympho)) %>%
  pivot_longer(cols = all_of(vars_Lympho), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Lympho))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de linfocitos por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor linfocitos",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Lympho)) %>%
  pivot_longer(cols = all_of(vars_Lympho), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Lympho))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de linfocitos por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor linfocitos",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_Lympho))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (linfocitos)")

Plaquetas

# Variables longitudinales de plaquetas
vars_Platelets <- c("Platelets", "Platelets_1C", "Platelets_2C", "Platelets_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_Platelets)) %>%
  pivot_longer(cols = all_of(vars_Platelets), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Platelets))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de plaquetas por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor plaquetas",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_Platelets)) %>%
  pivot_longer(cols = all_of(vars_Platelets), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_Platelets))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución de plaquetas por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor plaquetas",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_Platelets))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (plaquetas)")

NLR

# Variables longitudinales de NLR
vars_NLR <- c("NLR_pre", "NLR_1C", "NLR_2C", "NLR_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_NLR)) %>%
  pivot_longer(cols = all_of(vars_NLR), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_NLR))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del NLR por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor NLR",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_NLR)) %>%
  pivot_longer(cols = all_of(vars_NLR), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_NLR))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del NLR por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor NLR",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_NLR))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (NLR)")

Podriamos guardar NLR_pre y resumir el resto, ya que la diferencia es significativa, no tienen casi correlación.

PLR

# Variables longitudinales de PLR
vars_PLR <- c("PLR_pre", "PLR_1C", "PLR_2C", "PLR_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_PLR)) %>%
  pivot_longer(cols = all_of(vars_PLR), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_PLR))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del PLR por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor PLR",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_PLR)) %>%
  pivot_longer(cols = all_of(vars_PLR), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_PLR))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del PLR por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor PLR",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_PLR))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (PLR)")

Igual que NLR, podemos guardar la de prep y resumir el resto.

PNI

# Variables longitudinales de PNI
vars_PNI <- c("PNI_pre", "PNI_1C", "PNI_2C", "PNI_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_PNI)) %>%
  pivot_longer(cols = all_of(vars_PNI), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_PNI))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del PNI por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor PNI",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_PNI)) %>%
  pivot_longer(cols = all_of(vars_PNI), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_PNI))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del PNI por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor PNI",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_PNI))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (PNI)") 

Tenemos a PNI_1C como un punto intermedio, en cuanto a PNI_2C y PNI_1eval, la correlación es alta.

SII

# Variables longitudinales de SII
vars_SII <- c("SII_pre", "SII_1C", "SII_2C", "SII_1eval")

# =====================
# GRÁFICO 1 - Color por Mejor_resp
# =====================
df_long1 <- data %>%
  select(Patient_ID, Mejor_resp, all_of(vars_SII)) %>%
  pivot_longer(cols = all_of(vars_SII), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_SII))

g1 <- ggplot(df_long1, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(Mejor_resp),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>Mejor respuesta:", Mejor_resp
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del SII por paciente",
    subtitle = "Color según mejor respuesta al tratamiento",
    x = "Momento",
    y = "Valor SII",
    color = "Mejor respuesta"
  )

ggplotly(g1, tooltip = "text")
# =====================
# GRÁFICO 2 - Color por First_eval
# =====================
df_long2 <- data %>%
  select(Patient_ID, First_eval, all_of(vars_SII)) %>%
  pivot_longer(cols = all_of(vars_SII), names_to = "Momento", values_to = "Valor") %>%
  mutate(Momento = factor(Momento, levels = vars_SII))

g2 <- ggplot(df_long2, aes(
  x = Momento,
  y = Valor,
  group = Patient_ID,
  color = as.factor(First_eval),
  text = paste(
    "Paciente:", Patient_ID,
    "<br>Momento:", Momento,
    "<br>Valor:", round(Valor, 2),
    "<br>1ª evaluación:", First_eval
  )
)) +
  geom_line(alpha = 0.7) +
  geom_point(size = 1.5, alpha = 0.9) +
  theme_minimal() +
  labs(
    title = "Evolución del SII por paciente",
    subtitle = "Color según 1ª evaluación",
    x = "Momento",
    y = "Valor SII",
    color = "1ª evaluación"
  )

ggplotly(g2, tooltip = "text")
# =====================
# MATRIZ DE CORRELACIÓN
# =====================
df_corr <- data %>% select(all_of(vars_SII))

ggcorr(df_corr,
       label = TRUE,
       label_round = 2,
       label_size = 4,
       low = "steelblue", high = "red",
       name = "Correlación") +
  ggtitle("Matriz de correlación (SII)")

Igual que con las otras, variables, SII_prep no muestra una clara correlación, en cambio, SII_1C, SII_2C, SII_1eval se podrían resumir en una sola variable.